import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:fluwx/fluwx.dart' as fluwx;
import 'package:gzmall_app/common/api.dart';
import 'package:gzmall_app/common/toast_util.dart';

class ShareUtil {
  static showShareSelect(BuildContext context, String url, String content,
      String description, String image, String wxTransaction) {
    if (url == null || url.isEmpty || image == null || image.isEmpty) {
      ToastUtil.showMessageToast("分享参数数据有误");
      return;
    }
    fluwx.registerWxApi(appId: Api.wxAppId);
    showModalBottomSheet(
      context: context,
      builder: (BuildContext context) {
        return getShareDialog(
            context, url, content, description, image, wxTransaction);
      },
    );
  }

  static Widget getShareDialog(BuildContext context, String url, String content,
      String description, String image, String wxTransaction) {
    if (url == null || url.isEmpty || image == null || image.isEmpty) {
      ToastUtil.showMessageToast("分享参数数据有误");
      return Container();
    }
    fluwx.registerWxApi(appId: Api.wxAppId);
    return Container(
      padding: const EdgeInsets.only(
          top: 30.0, left: 30.0, right: 30.0, bottom: 120.0),
      color: Colors.white,
      child: Row(
        mainAxisAlignment: MainAxisAlignment.start,
        children: <Widget>[
          InkWell(
            onTap: () {
              shareWXTimeLine(url, content, image, description, wxTransaction);
              Navigator.of(context).pop();
            },
            child: SizedBox(
              height: 100.0,
              child: Column(
                children: <Widget>[
                  Image.asset(
                    'images/wx_time_line.png',
                    width: 30.0,
                    height: 30.0,
                  ),
                  Padding(
                    padding: const EdgeInsets.only(top: 5.0),
                    child: Text("朋友圈"),
                  ),
                ],
              ),
            ),
          ),
          Padding(
            padding: const EdgeInsets.only(left: 30.0),
            child: InkWell(
              onTap: () {
                shareWXSession(url, content, image, description, wxTransaction);
                Navigator.of(context).pop();
              },
              child: SizedBox(
                height: 100.0,
                child: Column(
                  children: <Widget>[
                    Image.asset(
                      'images/wx_session.png',
                      width: 30.0,
                      height: 30.0,
                    ),
                    Padding(
                      padding: const EdgeInsets.only(top: 5.0),
                      child: Text("微信好友"),
                    ),
                  ],
                ),
              ),
            ),
          ),
        ],
      ),
    );
  }

  static shareWXTimeLine(String url, String content, String image,
      String description, String wxTransaction) {
    if (url == null || url.isEmpty || image == null || image.isEmpty) {
      ToastUtil.showMessageToast("分享参数数据有误");
      return;
    }
    fluwx.registerWxApi(appId: Api.wxAppId);
    var model = fluwx.WeChatShareWebPageModel(
        url,
        title: content,
        thumbnail: fluwx.WeChatImage.network(image),
        scene: fluwx.WeChatScene.TIMELINE,
        description: description);
    fluwx.shareToWeChat(model);
  }

  static shareWXSession(String url, String content, String image,
      String description, String wxTransaction) {
    if (url == null || url.isEmpty || image == null || image.isEmpty) {
      ToastUtil.showMessageToast("分享参数数据有误");
      return;
    }
    fluwx.registerWxApi(appId: Api.wxAppId);
    var model = fluwx.WeChatShareWebPageModel(
        url,
        title: content,
        thumbnail: fluwx.WeChatImage.network(image),
        scene: fluwx.WeChatScene.SESSION,
        description: description);
    fluwx.shareToWeChat(model);
  }
}
